<template>
  <el-container>
    <el-header>
      <div>
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollap?'64px':'200px'">
        <div class="collapse-button" @click="isCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false" :collapse='isCollap' router
        >
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--图标-->
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/'+list.path" v-for="list in item.children" :key="">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{list.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Home",
    created() {
      this.getMenuList()
    },
    data() {
      return {
        menuList: [],
        //定义一级菜单的图标
        iconsList: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-lock_fill',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-users',
          '145': 'iconfont icon-danju',
        },
        isCollap: false
      }
    },
    methods: {
      exit() {
        //点击退出按钮时先退出token
        window.sessionStorage.clear()
        // 然后跳转到登录界面
        this.$router.push('/login')
      },
      async getMenuList() {
        const {data: res} = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error('获取列表失败')
        
        this.menuList = res.data
        // console.log(res)
      },
      isCollapse() {
        this.isCollap = !this.isCollap
      }
    }
  }
</script>

<style scoped lang="less">
  .el-container {
    height: 100%;
  }
  
  .el-header {
    width: 100%;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
  }
  
  .el-aside {
    background-color: #333744;
    
    .el-menu {
      border-right: none;
    }
    
    .collapse-button {
      color: #fff;
      font-size: 10px;
      line-height: 24px;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
  
  .el-main {
    background-color: #fff;
  }
</style>
